<template>  
  <div class="foot">
    <div class="player-content">
      <audio :src="audio.location" id="audioPlay" @canplay="canPlaySong" @timeupdate="timeUpdate" @error="loadError"  />
      <div class="cover" @click="showDetails">
        <div v-show="loading" class="loading-logo">
          <mu-circular-progress :size="30" />
        </div>
        <img :src="audio.albumPic" v-show="!loading" :alt="audio.name">
      </div>
      <div class="info">
        <div class="name">{{ audio.name }}</div>
        <div class="artist">{{ audio.singer }}</div>
      </div>
      <div class="controls">
        <mu-icon-button class="mini-btn player-list" @click="showList" />
        <mu-icon-button class="mini-btn player" :class="{pause: playing}" @click="toggleStatus"  />
        <mu-icon-button class="mini-btn next"  @click="next" />
      </div>
      <div class="pro"> 
        <div class="pro-play" :style="{'-webkit-transform':'translateX(' + prCurrentTime + '%)'}"></div>
      </div>
      <PlayList ref="bottomSheet"></PlayList>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import PlayList from '../components/playList'
export default {
  data () {
    return {
      player: '333'
    }
  },
  components: {
    PlayList
  },
  computed: {
    ...mapGetters([
      'audio',
      'loading',
      'playing',
      'change',
      'prCurrentTime',
      'tmpCurrentTime',
      'prCurrentTime'
    ])
  },
  methods: {
    canPlaySong () {
      this.$store.commit('closeLoading')
      document.getElementById('audioPlay').play()
      this.$store.commit('play')
    },
    toggleStatus () {
      if (this.playing === true) {
        document.getElementById('audioPlay').pause()
        this.$store.commit('pause')
      } else {
        document.getElementById('audioPlay').play()
        this.$store.commit('play')
      }
    },
    loadError () {
      if (document.getElementById('audioPlay').currentSrc) {
        this.loading = false
        this.$store.commit('closeLoading')
      } else {
        console.log('app程序第一次加载')
      }
    },
    next () {
      this.toggleStatus()
      this.$store.commit('playNext')
    },
    timeUpdate () {
      var myAudio = document.getElementById('audioPlay')
      var time = parseInt(myAudio.currentTime)
      this.$store.commit('updateDurationTime', parseInt(myAudio.duration))
      if (this.change) {
        myAudio.currentTime = this.tmpCurrentTime
        this.$store.commit('setChange', false)
      } else {
        this.$store.commit('updateCurrentTime', time)
      }
    },
    showList () {
      this.$refs.bottomSheet.show()
    },
    showDetails () {
      this.$router.push({name: 'playerDetail', params: {id: this.audio.id}})
      this.$store.commit('toggleDetail')
    }
  }
}
</script>

<style lang="less" scoped>
  @import "../assets/theme.less";
  .foot {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 998;
  }
  
  .player-content {
    width: 100%;
    height: 2.3rem;
    position: relative;
    background-color: rgba(255,255,255,.9);
    color: #333333;
    padding-left: 5px;
    padding-right: 5px;
  }

  .player-content .cover {
    width: 2rem;
    height: 2rem;
    overflow: hidden;
    float: left;
    background: url('../../static/player-bar.png') no-repeat;
    background-size: cover;
  }
  
  .player-content .cover img {
    width: 100%;
    height: 100%;
  }
  
  .player-content .cover .loading-logo {
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    background: #fff;
    text-align: center;
    padding-top: .4rem;
  }

  .player-content .info {
    position: relative;
    overflow: hidden;
    float: left;
    width: 7rem;
    padding-left: 0.3rem;
    padding-top: 0.2rem;
  }

  .player-content .info .name {
    font-size: .7rem;
    line-height: 1rem;
  }

  .player-content .info .artist {
    font-size: .6rem;
    color: #8a8a8a;
  }

  .player-content .info .name, .player-content .info .artist {
    overflow: hidden;
    display: block;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .player-content .controls {
    float: right;
  }
  
  .player-content .controls .mini-btn {
    width: 2rem;
    height: 2rem;
    background-size: cover!important;
  }

  .player-list {
    background: url("../../static/playbar_btn_playlist.png") no-repeat!important;
  }

  .player {
    background: url("../../static/playbar_btn_play.png") no-repeat!important;
  }

  .pause {
    background: url("../../static/playbar_btn_pause.png") no-repeat!important;
  }

  .next {
    background: url("../../static/playbar_btn_next.png") no-repeat!important;
  }

  .pro {
    width: 100%;
    height:.15rem;
    position: absolute;
    left: 0;
    bottom: 0;
    background-color: #ccc;
  }

  .pro .pro-play {
    width: 100%;
    height: .15rem;
    position: absolute;
    background-color: red;
    left: -100%;
  }

</style>
